﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class ParameterDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static ParameterDAO()
        {
            XmlConfigurator.Configure();
        }


        public static ParameterDTO GetParameter()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from p in context.PARAMETER_LIST
                            select p;
                ParameterDTO Result = new ParameterDTO();
                PARAMETER Parameter = query.First();
                Result = Mapper.Map<PARAMETER, ParameterDTO>(Parameter);
                return Result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static bool UpdateParameter(ParameterDTO dtoParameter)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                PARAMETER obj = (from p in context.PARAMETER_LIST
                                 select p).First();

                obj.GOAL_MAX_GOALTIME = dtoParameter.GOAL_MAX_GOALTIME;
                obj.GOAL_MAX_GOALTYPE = dtoParameter.GOAL_MAX_GOALTYPE;
                obj.MATCH_DRAW_SCORE = dtoParameter.MATCH_DRAW_SCORE;
                obj.MATCH_LOSE_SCORE = dtoParameter.MATCH_LOSE_SCORE;
                obj.MATCH_WIN_SCORE = dtoParameter.MATCH_WIN_SCORE;
                obj.PLAYER_MAX_AGE = dtoParameter.PLAYER_MAX_AGE;
                obj.PLAYER_MIN_AGE = dtoParameter.PLAYER_MIN_AGE;
                obj.TEAM_MAX_FOREIGNPLAYERS = dtoParameter.TEAM_MAX_FOREIGNPLAYERS;
                obj.TEAM_MAX_PLAYERS = dtoParameter.TEAM_MAX_PLAYERS;
                obj.TEAM_MIN_PLAYERS = dtoParameter.TEAM_MIN_PLAYERS;

                context.SaveChanges(true);
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }
    }
}
